﻿using System;
using System.Globalization;
using System.Text;
using Controller;

public partial class PostDetail : System.Web.UI.Page
{
    public static string _sContent = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var uniqueId = Request.QueryString["UniqueId"];
            BindData(uniqueId);
        }
    }

    public void BindData(string uniqueId)
    {
        var controller = new PostController();
        var listPost = controller.FindPostbyUniqueId(uniqueId);
        var post = listPost[0];

        litheaderTitle.Text = post.Title;
        _sContent = post.Description;

        // set data for display
        litTieude.Text = post.Title;
        litView.Text = post.View.ToString(CultureInfo.InvariantCulture);
        litContent.Text = Server.HtmlDecode(post.Content);
        litTags.Text = SpilitTag(post.Tags);
        litDateUpdated.Text = post.LastDateUpdated.ToString();

        controller.UpdateView(post);
    }

    public string SpilitTag(string strTags)
    {
        var stringBuilder = new StringBuilder();
        string[] tags = strTags.Split(',');
        foreach (var tag in tags)
        {
            stringBuilder.Append("<a class=\"badge\" href=\"../label/" + tag + "\">" + tag + "</a> ");
        }

        return stringBuilder.ToString();
    }

}